/*
 * Project:     My Project Work, Copyright (C) 2011
 * Created at:  13.08.2011
 */
package de.myprojectwork.model;

import de.myprojectwork.model.dto.Todo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

/**
 *
 * @author Chris Holdschuh
 */
public class TodoTreeTableModel extends AbstractTreeTableModel {

    public TodoTreeTableModel(List<Todo> list) {
        super(null);
        TodoTreeTableNode root = new TodoTreeTableNode(new Todo());
        for (Todo todo : list) {
            if (todo.getFkParent() == 0) {
                TodoTreeTableNode node = new TodoTreeTableNode(todo);
                node.getChildren().addAll(getChildren(list, node));
                root.getChildren().add(node);
            }
        }
        this.root = root;
    }
    
    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "!";
            case 1: return "Viel zu tun...";
            case 2: return "[%]";
            default: return "Unknown";
        }
    }

    @Override
    public int getHierarchicalColumn() {
        return 1;
    }
    
    @Override
    public Object getValueAt(Object node, int column) {
        TodoTreeTableNode treenode = (TodoTreeTableNode)node;
        switch(column) {
            case 0: return treenode.isLate() ? "!" : "";
            case 1: return treenode.getText();
            case 2: return treenode.getStatusPercent();
            default: return "Unknown";
        }
    }

    @Override
    public Object getChild(Object parent, int index) {
        TodoTreeTableNode treenode = (TodoTreeTableNode) parent;
        return treenode.getChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        TodoTreeTableNode treenode = (TodoTreeTableNode) parent;
        return treenode.getChildren().size();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        TodoTreeTableNode treenode = (TodoTreeTableNode) parent;
        for(int i = 0; i > treenode.getChildren().size(); i++) {
            if(treenode.getChildren().get(i) == child) {
                return i;
            }
        }
        return 0;
    }
    
    private List<TodoTreeTableNode> getChildren(List<Todo> all, TodoTreeTableNode parent) {
        List<TodoTreeTableNode> result = new ArrayList<>();
        if (all != null && parent != null) {
            for (Todo todo : all) {
                if (todo.getFkParent() == parent.getTodo().getId()) {
                    TodoTreeTableNode node = new TodoTreeTableNode(todo);
                    node.getChildren().addAll(getChildren(all, node));
                    result.add(node);
                }
            }
        }
        return result;
    }
    
    public class TodoTreeTableNode {

        private final int DAYS_IS_LATE = 3;
        
        private Todo todo;
        private List<TodoTreeTableNode> children;
        
        public TodoTreeTableNode(Todo todo) {
            this.todo = todo;
            children = new ArrayList<>();
        }
        
        public Todo getTodo() {
            return todo;
        }
        
        public List<TodoTreeTableNode> getChildren() {
            return children;
        }
        
        public String getText() {
            return todo.getText();
        }
        
        public int getStatusPercent() {
            return todo.getStatusPercent();
        }
        
        public boolean isLate() {
            if (todo.getTargetDate() != null) {
                Calendar today = GregorianCalendar.getInstance();
                Calendar target = GregorianCalendar.getInstance();
                target.setTime(todo.getTargetDate());

                int daysBetween = 0;
                while (today.before(target)) {
                    daysBetween++;
                    today.add(Calendar.DAY_OF_MONTH, 1);
                }
                return daysBetween < DAYS_IS_LATE;
            } else {
                return false;
            }
        }
    }
}
